﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Autodesk.AutoCAD.Windows;
using Autodesk.AutoCAD.Runtime;
using Autodesk.AutoCAD.Geometry;
using Autodesk.AutoCAD.EditorInput;
using Autodesk.AutoCAD.DatabaseServices;
using Autodesk.AutoCAD.ApplicationServices;
using ACADApplication = Autodesk.AutoCAD.ApplicationServices.Application;
using ACADDatabase = Autodesk.AutoCAD.DatabaseServices.Database;
using ACADDocument = Autodesk.AutoCAD.ApplicationServices.Document;
namespace YNCNumberSys
{
    public class ZJGetCurrentLayerName
    {
        /// <summary>
        /// 事务,为空表示操作当前图纸空间.
        /// </summary>
        private Transaction _trs_Transaction;
        /// <summary>
        /// 图纸文档WorkingDatabase,为空表示当前的图纸空间.
        /// </summary>
        private ACADDatabase _wdb_DataBase;
        /// <summary>
        /// 该值如果为True,则(1)_trs_Transaction,_wdb_DataBase和图纸文档均为当前图纸空间,并且图纸文档未锁定;
        /// (2)事务提交在该类内完成.
        /// </summary>
        private bool _b_TransactionIsUsed;
        public ZJGetCurrentLayerName(ACADDatabase wdb_DataBase, Transaction trs_Transaction)
        {
            _wdb_DataBase = wdb_DataBase;
            _trs_Transaction = trs_Transaction;
            _b_TransactionIsUsed = true;
        }

        public ZJGetCurrentLayerName()
        {
            _wdb_DataBase = HostApplicationServices.WorkingDatabase; ;
            _trs_Transaction = null;
            _b_TransactionIsUsed = false;
        }

        public string Do()
        {
            return (string)ACADApplication.GetSystemVariable("CLAYER");
        }

        public bool HasLayer(string layerName)
        {
            bool flag = false;

            if (!_b_TransactionIsUsed)
            {
                using (_trs_Transaction = _wdb_DataBase.TransactionManager.StartTransaction())
                {
                    try
                    {
                        LayerTable 层表;
                        层表 = (LayerTable)_trs_Transaction.GetObject(_wdb_DataBase.LayerTableId, OpenMode.ForRead);
                        if (层表.Has(layerName))
                        {
                            flag = true;
                        }
                        _trs_Transaction.Dispose();
                    }

                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        flag = false;
                        _trs_Transaction.Dispose();
                        //Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message);
                    }
                }
            }
            else
            {
                using (_trs_Transaction)
                {
                    try
                    {
                        LayerTable 层表;
                        层表 = (LayerTable)_trs_Transaction.GetObject(_wdb_DataBase.LayerTableId, OpenMode.ForRead);
                        if (层表.Has(layerName))
                        {
                            flag = true;
                        }
                    }

                    catch (Autodesk.AutoCAD.Runtime.Exception ex)
                    {
                        flag = false;
                        //Autodesk.AutoCAD.ApplicationServices.Application.ShowAlertDialog(ex.Message);
                    }
                }
            }
            return flag;
        }
    }
}
